package com.matt.sooncleaner.junk;

import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.RemoteException;
import android.util.Log;

import com.blankj.utilcode.util.Utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class IpakageStatatsTools {
    public boolean runObserver;
    private int mScanCount;
    private int mTotalCount;
    private ArrayList<JunkChildBean> mSysCaches = new ArrayList<>();
    private HashMap<String, String> mAppNames = new HashMap<>();
    private JunkBean systemJunkMotherBean;
    public long mTotalSize = 0L;
    public boolean timeOut;


    public void startObserver(ArrayList<JunkChildBean> mSysCaches, JunkBean systemJunkMotherBean) {
        this.mSysCaches = mSysCaches;
        this.systemJunkMotherBean = systemJunkMotherBean;
        runObserver = true;
        mTotalSize = 0;
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    PackageManager pm = Utils.getApp().getPackageManager();
                    List<ApplicationInfo> installedPackages = pm.getInstalledApplications(PackageManager.GET_GIDS);
                    IPackageStatsObserver.Stub observer = new PackageStatsObserver();
                    mScanCount = 0;
                    mTotalCount = installedPackages.size();
                    mAppNames = new HashMap<>();

                    for (int i = 0; i < mTotalCount; i++) {
                        ApplicationInfo info = installedPackages.get(i);
                        if (info.packageName != null && info.packageName.equals(Utils.getApp().getPackageName())) {
                            continue;
                        }
                        mAppNames.put(info.packageName, pm.getApplicationLabel(info).toString());
                        getPackageInfo(info.packageName, observer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

    private class PackageStatsObserver extends IPackageStatsObserver.Stub {
        @Override
        public void onGetStatsCompleted(PackageStats packageStats, boolean succeeded) throws RemoteException {
            if (!timeOut) {
                mScanCount++;
                if (packageStats == null || !succeeded) {
                } else {
                    JunkChildBean junkChildBean = new JunkChildBean();
                    junkChildBean.setName(packageStats.packageName);
                    junkChildBean.setSize(packageStats.cacheSize + packageStats.externalCacheSize);
                    if (junkChildBean.getSize() > 0) {
                        mSysCaches.add(junkChildBean);
                        mTotalSize += junkChildBean.getSize();
                        systemJunkMotherBean.setSize(mTotalSize);
                    }
                    Log.d(this.toString(), "packageStats.packageName " + packageStats.packageName + " " + junkChildBean.getSize());
                }

                if (mScanCount + 1 == mTotalCount) {
                    Collections.reverse(mSysCaches);
                    runObserver = false;
                }
            }
        }
    }

    public void getPackageInfo(String packageName, IPackageStatsObserver.Stub observer) {
        try {
            PackageManager pm = Utils.getApp().getPackageManager();
            Method getPackageSizeInfo = pm.getClass()
                    .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
            getPackageSizeInfo.invoke(pm, packageName, observer);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            //e.printStackTrace();
        }
    }
}